<?php
//dezend by http://www.yunlu99.com/ QQ:270656184
defined('BASEPATH') || exit('No direct script access allowed');
class MY_Controller extends CI_Controller
{
	protected $power;
	protected $my_power;

	public function __construct()
	{
		parent::__construct();
		date_default_timezone_set('PRC');
		$this->load->database();
		$this->load->library('session');
		$this->load->library('pwd');
		$this->load->library('d');
		$this->load->helper('url');
		$this->load->helper('num');
		$this->load->helper('chk');
		$this->load->helper('date');
		$this->load->helper('captcha');
		$this->load->helper('ajax_return');
		$this->load->helper('cookie');
		$this->load->helper('tool');
		$this->power = array(
	'coin'    => array(
		'name'  => '币种管理',
		'icon'  => 'icon-btc',
		'child' => array(
			'coin_1' => array('name' => '币种管理', 'url' => site_url('coin'))
			)
		),
	'item'    => array(
		'name'  => '项目管理',
		'icon'  => 'icon-trade',
		'child' => array(
			'item_1' => array('name' => '项目列表', 'url' => site_url('items')),
			'item_2' => array('name' => '投资记录', 'url' => site_url('support'))
			)
		),
	'push'    => array(
		'name'  => 'PUSH管理',
		'icon'  => 'icon-trade',
		'child' => array(
			'push_1' => array('name' => 'PUSH列表', 'url' => site_url('push')),
			'push_2' => array('name' => '交易记录', 'url' => site_url('deal')),
			'push_3' => array('name' => 'PUSH设置', 'url' => site_url('push_config'))
			)
		),
	'article' => array(
		'name'  => '文章管理',
		'icon'  => 'icon-list',
		'child' => array(
			'article_1' => array('name' => '文章分类', 'url' => site_url('article_class')),
			'article_2' => array('name' => '文章列表', 'url' => site_url('article'))
			)
		),
	'system'  => array(
		'name'  => '系统管理',
		'icon'  => 'icon-system',
		'child' => array(
			'system_1' => array('name' => '系统设置', 'url' => site_url('system')),
			'system_2' => array('name' => '财务设置', 'url' => site_url('money_config')),
			'system_3' => array('name' => '幻灯片设置', 'url' => site_url('slide')),
			'system_4' => array('name' => '合作伙伴设置', 'url' => site_url('partner')),
			'system_5' => array('name' => '友情链接设置', 'url' => site_url('frend'))
			)
		),
	'user'    => array(
		'name'  => '用户管理',
		'icon'  => 'icon-user',
		'child' => array(
			'user_1' => array('name' => '会员列表', 'url' => site_url('user')),
			'user_2' => array('name' => '实名认证', 'url' => site_url('pic_apply'))
			)
		),
	'admin'   => array(
		'name'  => '管理员管理',
		'icon'  => 'icon-admin',
		'child' => array(
			'admin_1' => array('name' => '管理员列表', 'url' => site_url('webmasters')),
			'admin_2' => array('name' => '权限管理', 'url' => site_url('power'))
			)
		),
	'xnb'     => array(
		'name'  => '财务管理',
		'icon'  => 'icon-finance',
		'child' => array(
			'xnb_1' => array('name' => '币账户管理', 'url' => site_url('xnb')),
			'xnb_2' => array('name' => '币提现地址管理', 'url' => site_url('address')),
			'xnb_3' => array('name' => '币转入管理', 'url' => site_url('fill_coin')),
			'xnb_4' => array('name' => '币转出管理', 'url' => site_url('draw_coin'))
			)
		)
	);
		$power_id = $this->d->field(array('field' => 'power_id', 'table' => 'webmasters', 'where' => 'id=' . intval($this->session->admin_id)));
		$this->my_power = $this->d->field(array('field' => 'actions', 'table' => 'power', 'where' => 'id=' . $power_id));
		$power_list = array();

		foreach ($this->power as $k => $v) {
			if (in_array($k, explode(',', $this->my_power))) {
				$power_list[$k] = $v;

				foreach ($v['child'] as $k1 => $v1) {
					if (in_array($k1, explode(',', $this->my_power))) {
						$power_list[$k][$k1] = $v1;
					}
				}
			}
		}

		$this->power = $power_list;
	}

	protected function verify_user()
	{
		if (!isset($this->session->admin_id) || (intval($this->session->admin_id) <= 0)) {
			redirect('login');
		}
	}

	protected function verify_url()
	{
		if (isset($_SERVER['HTTP_REFERER'])) {
			$from = parse_url($_SERVER['HTTP_REFERER']);
			$domain = domain($this->config->item('base_url'));

			if (!in_array($from['host'], array($domain, 'www.' . $domain))) {
				redirect('/error404.html');
			}
		}
		else {
			redirect('/error404.html');
		}
	}

	protected function view($t, $d = array())
	{
		$this->load->view('header');
		$this->load->view($t, $d);
		$this->load->view('footer');
	}

	protected function ajax_result($rs, $type, $is_lock = false)
	{
		if ($type == 'update') {
			$msg_suc = '修改成功';
			$msg_fail = '修改失败';
		}
		else if ($type == 'insert') {
			$msg_suc = '添加成功';
			$msg_fail = '添加失败';
		}
		else if ($type == 'delete') {
			$msg_suc = '删除成功';
			$msg_fail = '删除失败';
		}
		else {
			$msg_suc = '操作成功';
			$msg_fail = '操作失败';
		}

		if (chk($rs)) {
			if ($is_lock) {
				$this->db->query('commit');
				$this->db->query('unlock tables');
			}

			ajax_return(0, $msg_suc);
		}
		else {
			if ($is_lock) {
				$this->db->query('rollback');
				$this->db->query('unlock tables');
			}

			ajax_return(1, $msg_fail);
		}
	}

	protected function get_list($tablename, $where = array(), $order_by = 'add_time desc')
	{
		$page = intval($this->input->post('page'));
		$rows = intval($this->input->post('rows'));
		$id = intval($this->input->post('id'));
		$status = $this->input->post('status');
		$username = $this->input->post('username');
		$start = $this->input->post('start');
		$end = $this->input->post('end');

		if ($page <= 0) {
			$page = 1;
		}

		if ($rows <= 0) {
			$rows = 10;
		}

		if ($username) {
			if ($tablename != 'user') {
				$where['user_id'] = $this->d->find('id', 'user', array('username' => $username));
			}
			else {
				$where['username'] = $username;
			}
		}

		if ($start) {
			$where['add_time>='] = strtotime($start);
		}

		if ($end) {
			$where['add_time<='] = strtotime($end);
		}

		if ($id) {
			$where['id'] = $id;
		}

		if (-1 < $status) {
			$where['status'] = $status;
		}

		if (!$where) {
			$where[1] = 1;
		}

		$result['total'] = $this->db->where($where)->count_all_results($tablename);
		$result['rows'] = $this->db->where($where)->limit($rows, ($page - 1) * $rows)->order_by($order_by)->get($tablename)->result_array();
		return $result;
	}
}

?>
